using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Calendar.UI;

namespace Calendar
{
    public partial class frmLogin : Form
    {
        frmCalendarView frmCal;
        frmAlertController frmCtrlr;
        //frmAlertController frmCtrlr = new frmAlertController();

        public frmLogin()
        {
            InitializeComponent();
        }

        private void btnSignIn_Click(object sender, EventArgs e)
        {
            CalendarBLL bll = new CalendarBLL();
            GlobalInfo.loginStatus status= bll.Login(txtLogin.Text, txtPsw.Text);

            if (status == GlobalInfo.loginStatus.UserNotExist) MessageBox.Show("This User does not exist");
            else if (status == GlobalInfo.loginStatus.InvalidPassword) MessageBox.Show("Invalid Password");
            else if (status == GlobalInfo.loginStatus.SuccessfulLogin )
            {
                GlobalInfo.UserId = txtLogin.Text;
                this.ContextMenuStrip.Items[1].Enabled = false ; //sign in
                this.ContextMenuStrip.Items[2].Enabled = true; //sign out
                this.ContextMenuStrip.Items[3].Enabled = true; //calendar
                this.ContextMenuStrip.Items[4].Enabled = true; //alert settings
                this.ContextMenuStrip.Items[2].Text = "sign out (" + GlobalInfo.UserId + ")";
                //frmCal = new frmCalendarView();
                //frmCal.Show();
                this.Hide();
                this.Visible = false;
                notifyIcon1.Icon = new System.Drawing.Icon(Application.StartupPath + "\\Icon_03.ico");

                frmCtrlr = new frmAlertController();
                frmCtrlr.Show();
            }
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            notifyIcon1.Icon = new System.Drawing.Icon(Application.StartupPath + "\\Icon_02.ico");
            notifyIcon1.Visible = true;
            notifyIcon1.Text = "SMART Manager Calendar";

            this.Visible = false;
            this.ShowInTaskbar = false;
            this.WindowState = FormWindowState.Minimized;

            this.ContextMenuStrip.Items[1].Enabled = true ; //sign in
            this.ContextMenuStrip.Items[2].Enabled = false; //sign out
            this.ContextMenuStrip.Items[3].Enabled = false; //calendar
            this.ContextMenuStrip.Items[4].Enabled = false; //alert settings

        }

        private void signInToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Visible = true ;
            this.ShowInTaskbar =true ;
            this.WindowState = FormWindowState.Normal ;
            txtLogin.Focus();
        }

        private void signOutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(frmCal != null)    frmCal.Hide();
            this.Show();
            //signOutToolStripMenuItem.Enabled = false;
            //signInToolStripMenuItem.Enabled = true;
            this.ContextMenuStrip.Items[1].Enabled = true; //sign in
            this.ContextMenuStrip.Items[2].Enabled = false; //sign out
            this.ContextMenuStrip.Items[3].Enabled = false; //calendar
            this.ContextMenuStrip.Items[4].Enabled = false; //alert settings
            this.ContextMenuStrip.Items[2].Text = "sign out";
            GlobalInfo.UserId = "";
            GlobalInfo.UserRole = 0;
            notifyIcon1.Icon = new System.Drawing.Icon(Application.StartupPath + "\\Icon_02.ico");
        }

        private void calendarToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void txtLogin_KeyPress(object sender, KeyPressEventArgs e)
        {
            
        }

        private void txtPsw_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Convert.ToInt32(e.KeyChar) == 13)
                btnSignIn_Click(sender, e);
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            
        }

        private void calendarCMenu_Click(object sender, EventArgs e)
        {
            if (GlobalInfo.UserId.Equals("") == false) // user is logged in
            {
                frmCal = new frmCalendarView();
                //frmCal.ContextMenuStrip.Items["signInToolStripMenuItem"].Enabled = false;
                //frmCal.ContextMenuStrip.Items["signOutToolStripMenuItem"].Enabled = true;
                frmCal.Show();
                this.Hide();
                this.Visible = false;
            }
        }

        private void AlertSettingsCMenu_Click(object sender, EventArgs e)
        {
            if (GlobalInfo.UserId.Equals("") == false) // user is logged in
            {
                frmAlertSettings fas = new frmAlertSettings();
                fas.Show();
            }
        }
    }
}